﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace WinFormsApp3
{
    public partial class MainForm : Form
    {
       
        private OrderService orderService = new OrderService();
        private BindingSource orderBindingSource = new BindingSource();
        private BindingSource detailBindingSource = new BindingSource();

        public MainForm()
        {
            InitializeComponent();
            InitializeDataBindings();
            LoadOrders();
        }

        private void InitializeDataBindings()
        {
            // 设置主从数据绑定
            orderBindingSource.DataSource = typeof(Order);
            detailBindingSource.DataSource = orderBindingSource;
            detailBindingSource.DataMember = "Details";

            // 绑定主表到DataGridView
            dgvOrders.AutoGenerateColumns = false;
            dgvOrders.DataSource = orderBindingSource;

            // 绑定从表到Details DataGridView
            dgvDetails.AutoGenerateColumns = false;
            dgvDetails.DataSource = detailBindingSource;

            // 设置主表列
            dgvOrders.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "OrderId",
                HeaderText = "订单号",
                Name = "colOrderId"
            });

            dgvOrders.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "CustomerName",
                HeaderText = "用户名",
                Name = "colCustomer"
            });

            dgvOrders.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "OrderDate",
                HeaderText = "订单日期",
                Name = "colDate",
                DefaultCellStyle = new DataGridViewCellStyle() { Format = "yyyy-MM-dd" }
            });

            dgvOrders.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "TotalAmount",
                HeaderText = "总金额",
                Name = "colTotal",
                DefaultCellStyle = new DataGridViewCellStyle() { Format = "C2" }
            });

            // 设置从表列
            dgvDetails.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "ProductName",
                HeaderText = "商品",
                Name = "colProduct"
            });

            dgvDetails.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "UnitPrice",
                HeaderText = "单价",
                Name = "colUnitPrice",
                DefaultCellStyle = new DataGridViewCellStyle() { Format = "C2" }
            });

            dgvDetails.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Quantity",
                HeaderText = "数量",
                Name = "colQuantity"
            });

            dgvDetails.Columns.Add(new DataGridViewTextBoxColumn()
            {
                DataPropertyName = "Ammount",
                HeaderText = "总价",
                Name = "colAmount",
                DefaultCellStyle = new DataGridViewCellStyle() { Format = "C2" }
            });
        }

        private void LoadOrders()
        {
            var orders = orderService.QueryAllOrders();
            orderBindingSource.DataSource = orders;
            UpdateStatusBar();
        }

        private void UpdateStatusBar()
        {
            lblStatus.Text = $"Total Orders: {orderBindingSource.Count}";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var editForm = new OrderEditForm(null);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                orderService.AddOrder(editForm.EditedOrder);
                LoadOrders();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (orderBindingSource.Current == null)
            {
                MessageBox.Show("请选择订单后再进行编辑", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var selectedOrder = (Order)orderBindingSource.Current;
            var editForm = new OrderEditForm(selectedOrder);//跳转到编辑的界面
            if (editForm.ShowDialog() == DialogResult.OK)//更新
            {
                orderService.UpdateOrder(editForm.EditedOrder);
                LoadOrders();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (orderBindingSource.Current == null)
            {
                MessageBox.Show("请选择订单后再进行删除", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var selectedOrder = (Order)orderBindingSource.Current;
            if (MessageBox.Show($"确定要删除订单 {selectedOrder.OrderId}?", "Confirm",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                orderService.DeleteOrder(selectedOrder.OrderId);
                LoadOrders();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            var query = txtQuery.Text.Trim();
            if (string.IsNullOrEmpty(query))
            {
                LoadOrders();
                return;
            }

            var orders = orderService.QueryOrders(o =>
                o.OrderId.ToString().Contains(query) ||
                o.CustomerName.Contains(query, StringComparison.OrdinalIgnoreCase));

            orderBindingSource.DataSource = orders;
            UpdateStatusBar();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadOrders();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // 设置窗口初始大小和布局
            this.WindowState = FormWindowState.Maximized;
        }

       
    }
}
